//
//  KBTabBarController.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "KBTabBarController.h"
#import "LXMusicPlayerVC.h"
@interface KBTabBarController ()
@property (nonatomic, retain)KBCustomButton *previousBtn;
@property (nonatomic, retain)KBTabBarPlayButton *playBtn; // 播放按钮
@end
@implementation KBTabBarController
// 自定义设置tabBar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController{
    // 先删掉自己定义的
    [self.tabBarImageView removeFromSuperview];
    if (hidden){
        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        // 关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil){
            viewController.hidesBottomBarWhenPushed = YES;
        }
    }else{
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];
        // 关键***如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarImageView = [[UIImageView alloc] init];
    self.tabBarImageView.backgroundColor = [UIColor colorWithWhite:0.983 alpha:1.000];
    self.tabBarImageView.userInteractionEnabled = YES;
    self.tabBarImageView.image = [UIImage imageNamed:@""];
    
    [self isHiddenTabBar:NO withViewController:nil];
    
    // 下面的方法是调用自定义的生成按钮的方法
    [self creatButtonWithNormalName:@"tabbar_find_n@2x.png" andSelectName:@"tabbar_find_h@2x.png" andTitle:@"发现" andIndex:0];
    [self creatButtonWithNormalName:@"tabbar_sound_n@2x.png" andSelectName:@"tabbar_sound_h@2x.png" andTitle:@"定制" andIndex:1];
    [self creatButtonWithNormalName:@"tabbar_download_n@2x.png" andSelectName:@"tabbar_download_h@2x.png" andTitle:@"分类" andIndex:2];
    [self creatButtonWithNormalName:@"tabbar_me_n@2x.png" andSelectName:@"tabbar_me_h@2x.png" andTitle:@"我的" andIndex:3];
    // 默认选中第一个按钮
    KBCustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; // 自定义的控件中的按钮被点击了调用的方法,默认进入界面就选中第一个按钮
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeImage:) name:@"playImage" object:nil];
    self.playBtn = [[KBTabBarPlayButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/5, SCREEN_HEIGHT / 5)];
    [self.tabBarImageView addSubview:self.playBtn];
    [self.playBtn.musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)ChangeImage:(NSNotification*)notify{
    LXAuthorDetailModel *temp = [notify object];
    self.headImageLarge = temp.coverLarge;
    self.Title = temp.title;
    self.songId = temp.trackId;
}
#pragma mark - 此处跳转播放页面
-(void)buttonAction:(UIButton *)btn{
    LXMusicPlayerVC *player = [LXMusicPlayerVC sharaMusicPlayer];
    player.headImageLarge = self.headImageLarge;
    player.Title = self.Title;
    player.songId = self.songId;
    UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:player];
    [self presentViewController:navc animated:YES completion:nil];
}


#pragma mark - 创建按钮通用方法(自定义生成按钮方法)
- (void)creatButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index{
    KBCustomButton *button = [KBCustomButton buttonWithType:UIButtonTypeCustom];
    button.tag = index;
    [self.tabBarImageView addSubview:button];
    CGFloat buttonW = (SCREEN_WIDTH - (SCREEN_WIDTH / 375 * 200) - SCREEN_WIDTH * 0.18) / 4;
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_tabBarImageView.mas_bottom).offset(0);
        make.top.equalTo(_tabBarImageView.mas_top).offset(0);
        make.width.mas_equalTo(buttonW);
        if (index == 0) {
            make.left.mas_equalTo(SCREEN_WIDTH / 375 * 30);
        } else if (index == 1) {
            make.left.mas_equalTo(SCREEN_WIDTH / 375 * 75 + buttonW);
        } else if (index == 2) {
            make.right.mas_equalTo(-(SCREEN_WIDTH / 375 * 70 + buttonW));
        } else {
            make.right.mas_equalTo(-SCREEN_WIDTH / 375 * 30);
        }
    }];
    [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    // 设置按钮无效时的图片样式
    [button setImage:[UIImage imageNamed:selected] forState:UIControlStateDisabled];
    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];
}
#pragma mark - 按钮被点击时调用
- (void)changeViewController:(KBCustomButton *)sender{
    // ****关键,设置选中的控制器
    self.selectedIndex = sender.tag; // 切换不同控制器的界面
    sender.enabled = NO; //让选中的按钮变为不可用,也就是限制点击一次
    // 如果当前点击的按钮不是保存的按钮, 那么将按钮设置为可用状态.
    // 也就是两次点击的不是同一个按钮
    if (self.previousBtn != sender){
        self.previousBtn.enabled = YES;
    }
    // 记录当前点击的按钮
    self.previousBtn = sender;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end